﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Model;

namespace Web.admin.InnerManage.AwardManage
{
    public partial class SetPercent : System.Web.UI.Page
    {
        BLL.AwardService awardservice = new BLL.AwardService();
        BLL.ParticipantService participantservice = new BLL.ParticipantService();
        BLL.PercentService percentservice = new BLL.PercentService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["CON_ID"]))
                {
                    lb_Bind(Request.QueryString["CON_ID"]);
                    Rp_Bind();
                }
                else
                {
                    Response.Write("<script>alert('禁止修改地址')</script>");
                }
            }
            
        }
        /// <summary>
        ///根据竞赛绑定相应的label控件
        /// </summary>
        /// <param name="CON_ID"></param>
        protected void lb_Bind(string CON_ID)
        {
            BLL.ContestService contestservice = new BLL.ContestService();
            BoContests contestModel = contestservice.getContest(CON_ID);
            lb_ConName.Text = contestModel.CON_NAME;
        }

        /// <summary>
        /// 对repeater的绑定
        /// </summary>
        protected void Rp_Bind()
        {
            if (percentservice.GetCountByConId(Request.QueryString["CON_ID"]) > 0)
            {
                string constr = "PER_CONTEST ='" + Request.QueryString["CON_ID"] + "'";
                rp_update.DataSource = percentservice.Getv_Participant(constr);
                rp_update.DataBind();
                rp_award.Visible = false;
                rp_update.Visible = true;
            }
            else
            {
                rp_award.DataSource = awardservice.GetInnerAwards();
                rp_award.DataBind();
                rp_award.Visible = true;
                rp_update.Visible = false;
            }
        }

        /// <summary>
        /// 添加百分比
        /// </summary>
        /// <param name="sneder"></param>
        /// <param name="e"></param>
        protected void AddPersent(object sneder, EventArgs e)
        {
            if (rp_award.Visible == true && rp_update.Visible == false)
            {
                for (int i = 0; i < rp_award.Items.Count; i++)
                {
                    string awardId = ((HiddenField)rp_award.Items[i].FindControl("HF_id")).Value;
                    TextBox tb = (TextBox)rp_award.Items[i].FindControl("tb_persent");
                    TextBox tb_order = (TextBox)rp_award.Items[i].FindControl("tb_order");
                    if (!string.IsNullOrEmpty(tb.Text.Trim()) && !string.IsNullOrEmpty(tb_order.Text.Trim()))
                    {
                        BoPercent percentModel = new BoPercent();
                        percentModel.PER_CONTEST = Request.QueryString["CON_ID"];
                        percentModel.PER_AWARDS = awardId;
                        percentModel.PER_PERSENT = Convert.ToInt32(tb.Text.Trim());
                        percentModel.PER_ORDER = Convert.ToInt32(tb_order.Text.Trim());
                        percentservice.insert(percentModel);
                    }
                }

                UpDateStuAward();
                return;
            }
            else
            {
                for (int i = 0; i < rp_update.Items.Count; i++)
                {
                    string PercentId = ((HiddenField)rp_update.Items[i].FindControl("HF_id")).Value;
                    TextBox tb = (TextBox)rp_update.Items[i].FindControl("tb_persent");
                    TextBox tb_order = (TextBox)rp_update.Items[i].FindControl("tb_order");
                    if (!string.IsNullOrEmpty(tb.Text.Trim()) && !string.IsNullOrEmpty(tb_order.Text.Trim()))
                    {
                        BoPercent percentModel = percentservice.GetModelById(PercentId);
                        percentModel.PER_PERSENT = Convert.ToInt32(tb.Text);
                        percentModel.PER_ORDER = Convert.ToInt32(tb_order.Text);
                        percentservice.update(percentModel);
                    }
                }
                UpDateStuAward();
                return;
            }
            
        }

        /// <summary>
        /// 重新设置事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            if (percentservice.DeleteByConId(Request.QueryString["CON_ID"]))
            {
                Response.Write("<script>alert('重置成功')</script>");
                Rp_Bind();
            }
        }

        /// <summary>
        /// 更新学生的获奖信息
        /// </summary>
        protected void UpDateStuAward()
        {
            IList<BoParticipants> parList = participantservice.GetContestHasScoreParticipants(Request.QueryString["CON_ID"]);
            if (parList != null && parList.Count > 0)
            {
                if (cb_update.Checked)
                {
                    if (participantservice.innerAward(Request.QueryString["CON_ID"], participantservice.chekIsGroup(Request.QueryString["CON_ID"])))
                    {
                        Response.Write("<script>alert('百分比设置成功,并且学生获奖信息保存成功');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                        return;
                    }
                    else
                    {
                        Response.Write("<script>alert('百分比设置成功，但学生的获奖信息更改条数为0，请确保输入的合法性');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('百分比设置成功');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                    return;
                }
            }
            else
            {
                Response.Write("<script>alert('百分比设置成功，但是生成获奖等级失败\\n原因：未录入该竞赛的成绩');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                return;
            }
        }
    }
}